package me.algo.lst;

/**
 * @author guozheng
 * @date 2024/4/15
 */
public class L2 {

    public static void main(String[] args) {
        ListNode node = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5, new ListNode(6))))));
        ListNode node1 = removeElements(node, 1);
        System.out.println(node1);
    }
    /**
     * 203. 移除链表元素
     * @param head
     * @param val
     * @return
     */
    public static ListNode removeElements(ListNode head, int val) {
        ListNode rtn = new ListNode();
        rtn.next = head;
        ListNode cpr = rtn;
        ListNode cur = cpr.next;

        while (cur != null) {
            if (cur.val == val) {
                cur = cur.next;
                cpr.next = cur;
            } else {
                cur = cur.next;
                cpr = cpr.next;
            }
        }
        return rtn.next;
    }
}
